import { ref, watch } from 'vue'

export default (props: any, context: any) => {
  // 绑定控件的值
  const value: any = ref(props.modelValue)

  // 监听属性，给 value 赋值
  watch(() => props.modelValue, (newVal: string, oldVal: string) => {
    value.value = newVal
  })

  // 向父组件提交事件
  const changeVal = (value: string) => {
    context.emit('update:modelValue', value)
    context.emit('input', value)
  }

  return {
    value,
    changeVal
  }
}

